package com.oyesgo.statistics.thread;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Date;

import com.oyesgo.statistics.log.LogUtils;
import com.oyesgo.statistics.utils.DateFormat;
import com.oyesgo.statistics.utils.DateUtils;
import com.oyesgo.statistics.utils.MailFactory;
import com.rose.mail.SendMailOneRose;

/**
 * @FileName CheckThread.java
 * @Description: 检测线程，用于检测所有线程的活动状态
 * @author rose
 * @version 1.0
 * <p>Company: OYESGO</p>
 * <p>Copyright: Copyright (c) 2010</p>
 */
public class CheckThread extends Thread{
	@SuppressWarnings("unused")
	private ThreadGroup threadGroup;
	private Date startTime;
	private long n;
	public CheckThread(){}
	public CheckThread(ThreadGroup threadGroup,Date startTime,long n){
		this.threadGroup = threadGroup;
		this.startTime = startTime;
		this.n = n;
	}
	
	public void run() {
		while(true){
			if(threadGroup.activeCount() == 0){
				Date completeTime = new Date();
				long time = completeTime.getTime() - startTime.getTime();
				LogUtils.printlnToFile("[入库全部完成] [耗时"+DateFormat.getRunTimeInfo(time)+"] [共插入数据"+n+"条]");
				sendMailToRose();//发送邮件
				return;
			}
		}
	}
	
	/**
	 * 发送日志文件到邮箱
	 * void
	 * @author rose
	 */
	public static void sendMailToRose(){
		InputStream is = null;
		InputStreamReader isr = null;
		LineNumberReader lnr = null;
		StringBuffer contentSb = new StringBuffer();
		try {
			is = new FileInputStream(MailFactory.FILE_PATH);
			isr = new InputStreamReader(is);
			lnr = new LineNumberReader(isr);
			String strTemp = "";
			String newStr = null;
			try {
				while((strTemp = lnr.readLine()) != null){
					newStr = new String(strTemp.getBytes(),"GBK");
					if(!newStr.equals(strTemp)){
						strTemp = newStr;
					}
					contentSb.append(strTemp).append("\n");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			try {
				lnr.close();
				isr.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String emailContent = mailModel("自动入库日志",contentSb.toString(), "");
		SendMailOneRose sendMail = new SendMailOneRose();
		sendMail.setMailAddress(MailFactory.FROM_EMAIL_ADD);
		sendMail.setMailCount(MailFactory.FROM_EMAIL_USER);
		sendMail.setMailService(MailFactory.FROM_EMAIL_SERVICE);
		sendMail.setMailPassword(MailFactory.FROM_EMAIL_PWD);
		String[] emailStrs = MailFactory.getEmailAdds();
		for (int i = 0; i < emailStrs.length; i++) {
			sendMail.send(emailStrs[i], DateUtils.simpleDateFormat11(new Date())+"自动入库日志", emailContent);
		}
	}
	
	/**
	 * 邮件发送模板 本类调用
	 * @param title html的标题
	 * @param content 邮件内容
	 * @param name 对方称呼 群发时候可以写为 ""
	 * @return String 完整的html格式的邮件内容
	 * @author rose
	 */
	private static String mailModel(String title,String content,String name){
		StringBuffer modelSb = new StringBuffer();
		modelSb.append("<html><head><title>");
		modelSb.append(title);
		modelSb.append("</title></head>");
		modelSb.append("<body style='margin:20px 0 10px; font-size:12px; text-align:center; background:#CCECEC'>");
		modelSb.append("<div style='width:600px; margin:0 auto; text-align:left; padding:30px; background:#fff; border:1px'>");
		modelSb.append("<div style='background:#EAF3FA;'>");
		modelSb.append("</div>");
		modelSb.append("<div style='margin:6px 0 0; padding:0 5px'>");
		modelSb.append("<p>你好：");
		modelSb.append(name);
		modelSb.append("<br>");
		modelSb.append("自动入库日志文件内容：<br><span class='normal_orange14'>");
		modelSb.append(content);
		modelSb.append("</span></p>");
		modelSb.append("</div><hr/>");
		modelSb.append("<div><p style='line-height:150%'><font color='#0000FF'>cookie</font>&nbsp;|&nbsp;<font color='#0000FF'>自动入库</font>&nbsp;|&nbsp;<font color='#0000FF'>入库详细信息</font><br/></p></div>");
		modelSb.append("<div><p>&gt;&gt;</p></div>");
		modelSb.append("<divs tyle='margin:41px 0 0;padding:0 5px;line-height:18px;'><div style='margin:5px 3px 0 0;padding:0 5px;background:#EAF3FA;border-collapse:collapse;font-size:12px;color:#6B6B6B'>http://oa.oyesgo.net&nbsp;&nbsp;&nbsp;</div></div></div></body></html>");
		return modelSb.toString();
	}
	public static void main(String[] args) {
		CheckThread checkThread = new CheckThread();
		checkThread.sendMailToRose();
	}
}
